package com.zjdiepu.www.proxy.biz.impl.transfer;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.proxy.model.ext.transfer.TransferZxQueryCreditApplySubExt;
import com.zjdiepu.www.proxy.dao.transfer.TransferZxQueryCreditApplySubMapper;
import com.zjdiepu.www.proxy.model.dto.transfer.TransferZxQueryCreditApplySub;
import com.zjdiepu.www.proxy.biz.transfer.ITransferZxQueryCreditApplySubService;

/**
 * 中转_zx_征信查询申请子表Service层操作实现<br/>
 * @author xiongxiaotun<br/>
 * @time 2017年12月13日 17时50分<br/>
 * @since 1.0<br/>
 */
@Service
public class TransferZxQueryCreditApplySubServiceImpl extends BaseService implements ITransferZxQueryCreditApplySubService {
	public static final Logger logger = LoggerFactory.getLogger(TransferZxQueryCreditApplySubServiceImpl.class);

	@Autowired
	private TransferZxQueryCreditApplySubMapper transferZxQueryCreditApplySubMapper;
	
	@Override
	public void saveUpdateAndDelete(TransferZxQueryCreditApplySub transferZxQueryCreditApplySub, OperateType operType) {
		switch (operType) {
		case add:
			transferZxQueryCreditApplySub.initInsertData(transferZxQueryCreditApplySub.getModifier(), transferZxQueryCreditApplySub.getModifierName());
			transferZxQueryCreditApplySubMapper.insert(transferZxQueryCreditApplySub);
			break;
		case edit:
			transferZxQueryCreditApplySub.initUpdateData(transferZxQueryCreditApplySub.getModifier(), transferZxQueryCreditApplySub.getModifierName());
			// 影响的记录数
			int updateCount = transferZxQueryCreditApplySubMapper.updateByPrimaryKeySelective(transferZxQueryCreditApplySub);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			transferZxQueryCreditApplySub.initDeleteData(transferZxQueryCreditApplySub.getModifier(), transferZxQueryCreditApplySub.getModifierName());
			// 影响的记录数
			int deleteCount = transferZxQueryCreditApplySubMapper.updateByPrimaryKeySelective(transferZxQueryCreditApplySub);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<TransferZxQueryCreditApplySub> selectByExt(TransferZxQueryCreditApplySubExt transferZxQueryCreditApplySubExt) {
		return transferZxQueryCreditApplySubMapper.selectByExt(transferZxQueryCreditApplySubExt);
	}
	
	@Override
	public int countByExt(TransferZxQueryCreditApplySubExt transferZxQueryCreditApplySubExt){
		return transferZxQueryCreditApplySubMapper.countByExt(transferZxQueryCreditApplySubExt);
	}
	
	@Override
	public TransferZxQueryCreditApplySub selectById(String id) {
		return transferZxQueryCreditApplySubMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			transferZxQueryCreditApplySubMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(TransferZxQueryCreditApplySub transferZxQueryCreditApplySub) {
		return transferZxQueryCreditApplySubMapper.countRefined(transferZxQueryCreditApplySub);
	}

	@Override
	public TransferZxQueryCreditApplySub selectRefined(TransferZxQueryCreditApplySub transferZxQueryCreditApplySub) {
		return transferZxQueryCreditApplySubMapper.selectRefined(transferZxQueryCreditApplySub);
	}
	
	@Override
	public int deletedRefined(TransferZxQueryCreditApplySub transferZxQueryCreditApplySub) {
		return transferZxQueryCreditApplySubMapper.deletedRefined(transferZxQueryCreditApplySub);
	}

	@Override
	public boolean repeat(TransferZxQueryCreditApplySub transferZxQueryCreditApplySub) {
		int count = countRefined(transferZxQueryCreditApplySub);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(transferZxQueryCreditApplySub.getId())) {
			return true;
		}
		TransferZxQueryCreditApplySub dbItem = selectRefined(transferZxQueryCreditApplySub);
		if(null == dbItem) {
			return false;
		}
		return !transferZxQueryCreditApplySub.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return transferZxQueryCreditApplySubMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}
	
	@Override
	public int insertBatch(List<TransferZxQueryCreditApplySub> transferZxQueryCreditApplySubs) {
		return transferZxQueryCreditApplySubMapper.insertBatch(transferZxQueryCreditApplySubs);
	}
}
